package com.forg.mvc.core.plugin;

import java.util.Map;

import org.apache.velocity.Template;
import org.apache.velocity.VelocityContext;
import org.apache.velocity.app.VelocityEngine;
import org.apache.velocity.runtime.RuntimeConstants;
import org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.forg.mvc.core.common.Constant;
import com.forg.mvc.core.common.PropUtil;

@Plugin(name="velocityPulgin",method="init")
public class VelocityPulgin {
    
    public static final String TPL_ROOT = PropUtil.getString("tpl.root","tpl") + Constant.BACKSLASH;
    
    public static final VelocityEngine ve = new VelocityEngine();
    
    public static final Logger logger = LoggerFactory.getLogger(VelocityPulgin.class);
    
    public static void init(){
        ve.setProperty(RuntimeConstants.RESOURCE_LOADER, "classpath");
        ve.setProperty("classpath.resource.loader.class", ClasspathResourceLoader.class.getName());
        ve.init();
        logger.debug("init VelocityPulgin done.");
    }
    
    
    public static VelocityContext  getContext(){
        return new VelocityContext();  
    }
    
    @SuppressWarnings("rawtypes")
    public static VelocityContext  getContext(Map map){
        return new VelocityContext(map);  
    }
    
    public static Template getTemplate(String templName){
        
        return ve.getTemplate(TPL_ROOT + templName);
    }
    
}
